package com.unity3d.spapp7;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EditText userNameEdit = findViewById(R.id.editTextTextPersonName);
        EditText passwordEdit = findViewById(R.id.editTextTextPersonName2);

        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            /**
             * @param view
             */
            @Override
            public void onClick(View view) {
                String userName = userNameEdit.getText().toString();
                String passWord = passwordEdit.getText().toString();


                if(userName.equals("hnucm")&&passWord.equals("123")){
                    //todo 跳转到主页面+保存用户名+登录状态

                    //SharedPreferences是接口，并不是类
                    /*
                    参数一：存储数据的文件名
                     */
                    //获取到
                    SharedPreferences sp = getSharedPreferences("user",MODE_PRIVATE);
                    SharedPreferences.Editor editor=sp.edit();
                    editor.putString("username","湖南中医药大学");
                    editor.putBoolean("isLogin",true);
                    //删除数据
                    //editor.remove("username");
                    editor.commit();//数据会以文件的形式保存在本地

                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);


                    Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();


                }else{
                    Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_LONG).show();
                }

            }
        });

    }
}